package com.yongninger.activity.shipsmentinfo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yongninger.activity.R;
import com.yongninger.dao.ShipmentsInfoDao;
import com.yongninger.vo.ShipmentsInfoVo;

public class ViewActivity extends Activity {
	private ShipmentsInfoDao shipmentsInfoDao;
	ShipmentsInfoVo shipmentsInfoVo;
	EditText sname;
	EditText scode;
	EditText scompany;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shipsment_view);
		shipmentsInfoDao = new ShipmentsInfoDao(getApplicationContext());
		Intent intent = getIntent();
		shipmentsInfoVo = (ShipmentsInfoVo) intent
				.getSerializableExtra("shipmentsInfoVo");
		sname = (EditText)findViewById(R.id.smname);
		sname.setText(shipmentsInfoVo.getSname());
		scode = (EditText)findViewById(R.id.smcode);
		scode.setText(shipmentsInfoVo.getScode());
		scompany = (EditText)findViewById(R.id.smcompany);
		scompany.setText(shipmentsInfoVo.getScompany());

		Button modifyBtn = (Button) this.findViewById(R.id.modify);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (update()) {
					Toast.makeText(getApplicationContext(), "修改成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "修改失败", 2).show();
				}
			}
		};
		modifyBtn.setOnClickListener(onClickListener);

		/*
		 * 删除按钮
		 */
		Button deleteBtn = (Button) this.findViewById(R.id.delete);
		OnClickListener onClickListener1 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 删除
				boolean result = shipmentsInfoDao.deleteShipmentsInfoVo(shipmentsInfoVo.getSid());
				if (result) {
					Toast.makeText(getApplicationContext(), "删除成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "删除失败", 2).show();
				}
			}
		};
		deleteBtn.setOnClickListener(onClickListener1);
	}

	private boolean update() {
		ShipmentsInfoVo shipmentsInfoVoTemp = new ShipmentsInfoVo();
		shipmentsInfoVoTemp.setScode(scode.getText().toString());
		shipmentsInfoVoTemp.setSname(sname.getText().toString());
		shipmentsInfoVoTemp.setScompany(scompany.getText().toString());
		return shipmentsInfoDao.updateShipmentsInfoVo(shipmentsInfoVoTemp, shipmentsInfoVo.getSid());
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Toast.makeText(getApplicationContext(), "请先保存内容",
					Toast.LENGTH_SHORT).show();
			return false;
		}else if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() >= 0){
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(ViewActivity.this,
					ListActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
